<template>
  <div class="wrap">
    <div class="ms-login" style="max-width: 600px">
      <div class="ms-title">南昌大学数学与计算机学院学生通讯录</div>
      <el-form
          :model="form"
          :rules="rules"
          ref="login"
          label-width="auto"
          class="ms-content"
          :size="'large'"
      >
        <el-form-item prop="id" size="large">
          <el-input v-model="form.id" placeholder="请输入用户名">
            <template #prepend>
              <el-button icon="el-icon-user"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password" size="large">
          <el-input
              type="password"
              placeholder="请输入密码"
              v-model="form.password"
              @keyup.enter="login()"
          >
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
          <el-checkbox v-model="rememberLogin">记住登录</el-checkbox>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
        <div class="register-btn">
          <el-button type="primary" @click="goRegisterPage">注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">

import {onMounted, reactive, ref} from "vue";
import {store} from "@/store";
import router from "@/router";
import {loginUser} from "@/api/user.ts";
import {LoginData} from "@/api/types/user.ts";
import {ElMessage} from "element-plus";

const rememberLogin = ref(false)
const form = reactive<LoginData>({
  id: '',
  password: '',
})

const rules = {
  id: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const goRegisterPage = () => {
  store.commit('cleanToken')
  router.push('/register')
}

const login = () => {
  if (form.id == "admin" && rememberLogin.value) {
    ElMessage('管理员禁止记住登录')
    rememberLogin.value = false
    return
  }
  return loginUser(form).then(data => {
    if (data.code == 100) {
      store.commit('setToken', data.token)
      store.commit('setId', form.id)

      if (rememberLogin.value) {
        store.commit('storeToLocalStorage')
      }

      router.replace('/')
    } else {
      ElMessage(data.msg)
    }
  })
}

onMounted(() => {
  store.commit('loadFromLocalStorage')
  if (store.state.token != '') {
    router.replace('/')
  }
})

</script>

<style scoped>
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #000000;
}
.ms-login {
  position: absolute;
  left: 45%;
  top: 35%;
  height: 50vh;
  width: 60vh;
  margin: -190px 0 0 -175px;
  border-radius: 15px;
  background-color: rgba(255, 255, 255, 0.86);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
  background-color: #36c486;
  border-color: #36c486;
}
.register-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
  background-color: rgba(229, 208, 39, 0.72);
  border-color: rgba(229, 208, 39, 0.72)
}
</style>
